package com.test.common;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import msg.chestionar.ejb.bi.ChestionarBI;
import msg.chestionar.ejb.bi.RaspunsBI;

import com.msg.chestionar.to.ChestionarTO;
import com.msg.chestionar.to.RaspunsuriTO;
import com.msg.chestionar.to.UtilizatorTO;

@ManagedBean(name = "administratorBean")
@SessionScoped
public class AdminBean implements Serializable {
	private static final long serialVersionUID = -4759076671753497857L;

	@EJB
	private ChestionarBI chestionarBI;
	@EJB
	private RaspunsBI raspunsBI;

	private List<ChestionarTO> chestionare;
	
	

	@ManagedProperty(value = "#{rezultateChBean}")
	private RezultateChestionarBean rezChestionarBean;

	@ManagedProperty(value = "#{editBean}")
	private EditBean editBean;

	public void initAdminBean() {
		this.chestionare = chestionarBI.getToateChestionarele();
	}

	public String edit() {
		FacesContext context = FacesContext.getCurrentInstance();
		String chestionarId = (String) context.getExternalContext()
				.getRequestParameterMap().get("chestionarSelected");
		ChestionarTO chestionar;
		if (chestionarId != null) {
			chestionar = chestionarBI.getChestionar(Long.valueOf(chestionarId));
			editBean.setChestionar(chestionar);
			editBean.setNume(chestionar.getNume());
			editBean.setActiv(chestionar.getActiv());
		}
		// TODO: daca ii null error checking
		return "editare_chestionar";
	}

	public String addChestionar() {
		editBean.setChestionar(new ChestionarTO());
		editBean.setNume("");
		editBean.setActiv(false);

		return "editare_chestionar";
	}

	public String delete() {

		FacesContext context = FacesContext.getCurrentInstance();

		String chestionarId = (String) context.getExternalContext()
				.getRequestParameterMap().get("chestionarSelected");

		ChestionarTO chestionar = chestionarBI.getChestionar(Long
				.valueOf(chestionarId));

		chestionare.remove(chestionar);
		chestionarBI.deleteChestionar(Long.valueOf(chestionarId));
		raspunsBI.deleteRaspunsuriChestionar(Long.valueOf(chestionarId));

		initAdminBean();
		return "administrator";

	}

	public String getRezultate() {

		FacesContext context = FacesContext.getCurrentInstance();
		String chestionarId = (String) context.getExternalContext()
				.getRequestParameterMap().get("rezultatePressed");

		ChestionarTO chestionar = chestionarBI.getChestionar(Long
				.valueOf(chestionarId));

		for (int i = 0; i < chestionar.getIntrebari().size(); i++) {
			if (this.raspunsBI.getRaspunsuriNumber(chestionar.getId(),
					chestionar.getIntrebari().get(i).getId()) != 0)
				chestionar.getIntrebari().get(i).setNrRaspunsuri(
						this.raspunsBI.getRaspunsuriNumber(
								Long.valueOf(chestionarId),
								chestionar.getIntrebari().get(i).getId())
								.toString());
			else
				chestionar.getIntrebari().get(i).setNrRaspunsuri("0");

			for (int j = 0; j < chestionar.getIntrebari().get(i).getOptiuni()
					.size(); j++)
				chestionar.getIntrebari().get(i).getOptiuni().get(j)
						.setProcentaj(
								this.raspunsBI.getProcentaj(Long
										.valueOf(chestionarId), chestionar
										.getIntrebari().get(i).getOptiuni()
										.get(j).getId()));

		}

		rezChestionarBean.setChestionar(chestionar);

		return "rezultate_chestionar";
	}

	public List<ChestionarTO> getChestionare() {
		if (this.chestionare == null) {
			this.initAdminBean();
		}
		return chestionare;
	}

	public String clear() {

		ChestionarTO chestionar = new ChestionarTO();
		chestionar.setActiv(false);
		chestionar.setIntrebari(null);
		chestionar.setNume("");

		return "editare_intrebare";

	}

	public String home() {

		// ChestionarTO chestionar = new ChestionarTO();
		// chestionar.setActiv(false);
		// chestionar.setIntrebari(null);
		// chestionar.setNume("");
		this.initAdminBean();
		return "administrator";
	}

	public void setChestionare(List<ChestionarTO> chestionare) {
		this.chestionare = chestionare;
	}

	public String logout() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		session.invalidate();
		return "login";
	}

	public void setRezChestionarBean(RezultateChestionarBean rezChestionarBean) {
		this.rezChestionarBean = rezChestionarBean;
	}

	public RezultateChestionarBean getRezChestionarBean() {
		return rezChestionarBean;
	}

	public EditBean getEditBean() {
		return editBean;
	}

	public void setEditBean(EditBean editBean) {
		this.editBean = editBean;
	}
	
	

}
